<template>
  <div class="typebig">
    <div class="topbox">
      <div>能源类型维护</div>
      <div class="butti">
        <span><el-button size="small" type="primary" @click="dialogVisible=true"><i class="el-icon-plus"></i>新增能源类型</el-button></span>
        <TopTooltip @command="command"  @shuaxin="shuaxin"></TopTooltip>
      </div>
    </div>
    <div class="tablebox">
      <TableColumn
        ref="tabledata"
        :tableData="tableData" 
        :tableConfig="tableConfig" 
        :issize="issize">
        <el-table-column
            prop="address"
            label="操作"
            align="center"
            width="150">
            <template #default="{row}">
              <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                <i @click="putlist(row)" class="el-icon-edit" style="color:#1a90fe ;margin-right: 20px; cursor: pointer;"></i>
               </el-tooltip>
               <el-tooltip class="item" effect="dark" content="删除" placement="top">
                <i @click="dellist(row)" class="el-icon-delete" style="color: red;cursor: pointer;"></i>
               </el-tooltip>
            </template>
          </el-table-column>
        </TableColumn>
        <!-- 底部分页 -->
        <div class="fenye">
          <el-pagination
            background
            layout="prev, pager, next"
            :current-page.sync="pageobj.pageNo"
            :page-size="15"
            @current-change="getdatalist"
            :total="+totalCount">
          </el-pagination>
        </div>
        <!-- 弹窗 -->
        <div class="dialog">
          <el-dialog
          title="能源类型维护"
          :visible.sync="dialogVisible"
          :show-close="false"
          width="40%">
          <div slot="title">能源类型维护<span style="float: right;font-size: 18px;padding: 0 10px;cursor: pointer;" @click="quxiao">
            <i class="el-icon-close"></i> </span>
          </div>
          <div>
            <el-form :model="form" label-position="right" label-width="80px" ref="formdata">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="用电单位" prop="companyId">
                    <el-select v-model="form.companyId" placeholder="请选择用电单位"  size="small" style="width: 100%;">
                      <el-option v-for="(item,index) in yonghulist" :key="index" :label="item.name" :value="item.id" class="option"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="年份" prop="year">
                    <el-date-picker v-model="form.year" type="year" value-format="yyyy" placeholder="选择年" size="small" style="width: 100%;"></el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="数据来源" prop="dataSources">
                    <el-select v-model="form.dataSources" placeholder="请选择数据来源"  size="small" style="width: 100%;">
                      <el-option v-for="(item,index) in shujulist" :key="index" :label="item.text" :value="item.value" class="option"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="能源分类" prop="energyClassify">
                    <el-select v-model="form.energyClassify" placeholder="请选择能源分类"  size="small" style="width: 100%;" @change="changefenlei">
                      <el-option v-for="(item,index) in nengyuanlist" :key="index" :label="item.text" :value="item.value" class="option"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="能源类型" prop="energyType">
                    <el-select v-model="form.energyType" placeholder="请选择能源类型"  size="small" style="width: 100%;" @change="$forceUpdate()">
                      <el-option v-for="(item,index) in leixinglist" :key="index" :label="item.text" :value="item.value" class="option"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="能源单位" prop="energyUnit">
                    <el-input v-model="form.energyUnit" placeholder="请输入用电单位" size="small" style="width: 100%;"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="折标系数" prop="ratio">
                    <el-input v-model="form.ratio" placeholder="请输入折标系数" size="small" style="width: 100%;">
                      <span slot="suffix" >kgce</span>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="碳排放" prop="carbonEmissionInfo">
                    <el-input v-model="form.carbonEmissionInfo" placeholder="请输入碳排放" size="small" style="width: 100%;"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="备注" prop="remark">
                    <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" size="small" style="width: 100%;"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="quxiao">取 消</el-button>
            <el-button size="small" type="primary" @click="submit">确 定</el-button>
          </span>
        </el-dialog>
        </div>
    </div>
  </div>
</template>

<script>
import {getzidianxialaapi} from '@/api/guidelines'
import TopTooltip from '@/components/TopTooltip/index.vue'
import TableColumn from '@/components/Table/index.vue'
import {gettypelistapi,addnengyuantypeapi,delnengyuantypeapi,putnengyuantypeapi,getyongdianlistapi} from '@/api/energy-consumption/Basic-information/medium'
export default {
  name: 'CallPoliceType',
  props: {
    companyId: {
      type: String,
    },
    year: {
      type: String,
    }
  },
  components: {
    TopTooltip,
    TableColumn
  },
  data() {
    return {
      yonghulist:[],
      issize: 'medium',
      tableData: [],
      tableConfig: [
        { label: '序号', type: 'index', center: 'center', width: '100' },
        { label: '单位名称', prop: 'companyName',  },
        { label: '年份', prop: 'year', center: 'center' },
        { label: '能源类型', prop: 'energyTypeName',},
        { label: '单位', prop: 'energyUnit',},
        { label: '折标系数', prop: 'ratio', center: 'center' },
        { label: '碳排放信息(参考能源类型维护)', prop: 'carbonEmissionInfo',width: '250'  },
        { label: '数据来源', prop: 'dataSourcesName',   }
      ],
      totalCount: '',
      pageobj: {
        pageNo: 0,
        pageSize:15
      },
      dialogVisible:false,
      form:{},
      shujulist:[],
      nengyuanlist:[],
      leixinglist:[]
    };
  },

  mounted() {
    
  },

  methods: {
    // 弹窗确认
    async submit() {
      this.form.id?await putnengyuantypeapi(this.form).then(res => {
        this.$message({
          type: res.code == 200 ? 'success' : 'warning',
          message:res.code == 200 ?'修改成功':res.message
        })
        this.quxiao()
        this.getdatalist()
      }):
      await addnengyuantypeapi(this.form).then(res => {
        this.$message({
          type: res.code == 200 ? 'success' : 'warning',
          message:res.code == 200 ?'新增成功':res.message
        })
        this.quxiao()
        this.getdatalist()
      })
    },
    // 修改
    async putlist(row) {
      this.dialogVisible = true
      await this.changefenlei(row.energyClassify)
      this.form = { ...row }
      this.form.year=row.year.toString()
    },
    // 删除
    dellist(row) {
      this.$confirm('此操作将永久删除该配置, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await delnengyuantypeapi({ id: row.id }).then(res => {
            this.$message({
              type: res.code == 200 ? 'success' : 'warning',
              message:res.code == 200 ?'删除成功':res.message
            })
          })
          this.getdatalist()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 弹窗取消
    async quxiao() {
      this.form.id=''
      this.$refs.formdata.resetFields()
      this.dialogVisible = false
    },
    // 获取表格数据
    async getdatalist() {
      this.$refs.tabledata.loading=true
      const { data: { list ,totalCount} } = await gettypelistapi({
        pageNo: this.pageobj.pageNo - 1,companyId:this.companyId,
        pageSize: this.pageobj.pageSize,year:this.year,
      })
      this.tableData = list
      this.totalCount=totalCount
      this.$refs.tabledata.loading=false
    },
    // 能源分类下拉
    async changefenlei(val) {
       // 获取能源类型并筛选 截取字符串前两个筛选出选中分类下所属类型
      const { data: { list } } = await getzidianxialaapi({ dictType: 'EnergySubType' })
      this.leixinglist = list.filter(ele => {
         return ele.value.slice(0, 2)==val
      })
      this.form.energyType=''
    },
   // 调节表格密度
  command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
    // 列表刷新
    shuaxin() {
      this.getdatalist()
      this.$message.success('列表刷新成功')
    },
  },
  watch: {
    async dialogVisible(val) {
      if (val) {
        // 获取单位
        const {data:{list}} = await getyongdianlistapi()
        this.yonghulist=list
       // 获取收费类型
        const shuju = await getzidianxialaapi({ dictType: 'DataFrom' })
        this.shujulist = shuju.data.list
       // 获取能源分类
       const nengyuan = await getzidianxialaapi({ dictType: 'EnergyType' })
        this.nengyuanlist = nengyuan.data.list
      }
    }
  }
};
</script>

<style scoped>
.option{
  padding-left: 10px;
}
.typebig{
  height: 100%;
  .topbox{
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      .butti{
        display: flex;
      }
    }
    .tablebox{
      height: 88%;
      .fenye{
        text-align: end;
        margin-top: 10px;
      }
      .dialog{
        .button{
          width: 60px;
          height: 30px;
        }
      }
    }
}
::v-deep .el-row{
  margin-bottom: 10px;
}
::v-deep .el-dialog__footer{
  text-align: center;
}
</style>